package com.example.administrator.live_demo;


import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.baidu.recorder.api.LiveConfig;
import com.baidu.recorder.api.LiveSession;
import com.baidu.recorder.api.LiveSessionHW;
import com.baidu.recorder.api.LiveSessionSW;
import com.baidu.recorder.api.SessionStateListener;

public class MainActivity extends AppCompatActivity {

    private LiveSession liveSession;
    private SurfaceView sfv;
    private Button start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().hide();
        init();
        initRTMP();

    }

    public void initRTMP() {
        //定义liveConfig
        LiveConfig liveConfig=new LiveConfig.Builder()
                .setCameraId(LiveConfig.CAMERA_FACING_FRONT)//设置前置摄像头
                .setCameraOrientation(LiveConfig.ORIENTATION_PORTRAIT)//设置为竖屏
                .setVideoWidth(1280)
                .setVideoHeight(720)
                .setVideoFPS(18)
                .setInitVideoBitrate(1024000)
                .setAudioEnabled(true)
                .setAudioBitrate(64*1000)
                .setAudioSampleRate(LiveConfig.AUDIO_SAMPLE_RATE_44100)
                .setGopLengthInSeconds(2)
                .build();
        //硬编码
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR2){
            liveSession=new LiveSessionHW(this,liveConfig);
        }else {
            //软编码
            liveSession=new LiveSessionSW(this,liveConfig);
        }
        liveSession.setStateListener(new SessionStateListener() {
            @Override
            public void onSessionPrepared(int i) {

            }

            @Override
            public void onSessionStarted(int i) {



            }

            @Override
            public void onSessionStopped(int i) {

            }

            @Override
            public void onSessionError(int i) {

            }
        });
        liveSession.bindPreviewDisplay(sfv.getHolder());
        liveSession.prepareSessionAsync();

    }

    private void init(){
        sfv= (SurfaceView) findViewById(R.id.sfv);
        start= (Button) findViewById(R.id.btn);
    }

    public void player(View view){
        if(start.getText().equals("开始直播")){
            liveSession.startRtmpSession("rtmp://192.168.25.15:1935/live/ll");
            start.setText("停止直播");
        }else {
            liveSession.stopRtmpSession();
            start.setText("开始直播");
        }
    }
}
